$(function(){
	init();
	initButton();
});
//初始化页面
function init(){
	$.ajax({
		type : "post",
		url : server_url
				+ 'rest/tea/product/getTeaShowList.do?ran=' + Math.random(),
		dataType : 'json',
		async : true,
		data :{
			token : token
		},
		success : function(result) {
			console.log(result);
			if (result.retCode == '0000') {
				showList(result.data);
			}else{
				showErrorMsg(result.data);
			}
		}
	});
}
//初始化绑定事件
function initButton(){
	//绑定进入明细事件
	$('body').on('click','.skill-list',function(){
		var type = $(this).attr('type');
		var productId = $(this).attr('productid');
		var param = {};
		jumpDetail(type,param,productId);
	});
	//绑定查询按钮
	$('body').on('click','.layui-btn',function(){
		$('.all-detail').css({
			'display': 'none'
		});
		var sex = $("input[type='radio'][name=sex]:checked").val();
		var age = $("input[type='radio'][name=age]:checked").val();
		var price = $("input[type='radio'][name=price]:checked").val();
		var hire = $("input[type='radio'][name=hire]:checked").val();
		var proType = $("input[type='radio'][name=proType]:checked").val();
		var location = $("#J_Address").val();
		if(!location||location==""){
			localthion = "";
		}else{
			var locations = location.split(" ");
			if(locations[0]=="北京"||locations[0]=="天津"||locations[0]=="上海"||locations[0]=="重庆"){
				location = locations[1].substring(0,2);
			}else{
				location = locations[2].substring(0,2);
			}
		}
		$.ajax({
			type : "post",
			url : server_url
					+ 'rest/tea/product/getTeaShowList.do?ran=' + Math.random(),
			dataType : 'json',
			async : true,
			data :{
				token : token,
				sex:sex,
				age:age,
				price:price,
				hire:hire,
				proType:proType,
				location:location,
			},
			success : function(result) {
				console.log(result);
				if (result.retCode == '0000') {
					showList(result.data);
				}else{
					showErrorMsg(result.data);
				}
			}
		});
	});
	//绑定销量查询按钮
	$('body').on('click','.sell',function(){
		$('.all-detail').css({
			'display': 'none'
		});
		$('.price-item').removeAttr("style");
		$('.all-item').removeAttr("style");
		$('.sell-item').attr('style',"color:#01cb72");
		searchProList("tea_sell_num","desc");
	});
	//绑定价格查询按钮
	$('body').on('click','.price',function(){
		$('.all-detail').css({
			'display': 'none'
		})
		$('.sell-item').removeAttr("style");
		$('.all-item').removeAttr("style");
		$('.price-item').attr('style',"color:#01cb72");
		var h = $(this).children().html();
		if(h=="价格由低到高"){
			$(this).children().html('价格由高到低');
			searchProList("tea_price","desc");
		}else{
			$(this).children().html('价格由低到高');
			searchProList("tea_price","asc");
		}
	});
}
//展示列表
function showList(productList){
	var str = '';
	$.each(productList,function(i,n){
		var teaIndustryStr = ''
		var teaIndustrys = n.teaIndustry.split(",");
		$.each(teaIndustrys,function(i,n){
			if(n==0){
				teaIndustryStr += '<span class="fl color02">茶艺师</span>';
			}else if(n==1){
				teaIndustryStr += '<span class="fl color03">评茶员（师）</span>';
			}else if(n==2){
				teaIndustryStr += '<span class="fl color02">制茶师</span>';
			}else if(n==3){
				teaIndustryStr += '<span class="fl color03">茶园园艺工</span>';
			}
		});
		var age = n.teaAge;
		var year = n.teaYear;
		var trip;
		if(n.teaTrip=='0'){
			trip = '不出差';
		}else{
			trip = '可出差';
		}
		str +=' <div class="skill-list clear" type="'+n.cateid+'" productid="'+n.productid+'"><img src="'+n.pic+'" alt="" class="fl"><div class="skill-box fr"><div class="item-det clear"><span class="fl name">'+n.productname+'</span>'+teaIndustryStr+'</div><div class="item-time clear"><span class="fl">女/'+n.teaAge+'岁/从业'+n.teaYear+'年/'+trip+'</span></div><div class="item-time clear"><span class="fl">成交量：'+n.teaSellNum+'</span><span class="fr">月薪面议</span></div><div class="num-box clear"><div class="fl">￥<span>'+n.priceretailguide+'</span>元</div><p class="fr">'+n.teaLocation+'</p></div></div></div>';
	});
	$('.skill-other').html(str);
}
//
function searchProList(column,sort){
	$.ajax({
		type : "post",
		url : server_url
				+ 'rest/tea/product/getTeaShowList.do?ran=' + Math.random(),
		dataType : 'json',
		async : true,
		data :{
			token : token,
			column:column,
			sort:sort
		},
		success : function(result) {
			console.log(result);
			if (result.retCode == '0000') {
				showList(result.data);
			}else{
				showErrorMsg(result.data);
			}
		}
	});
}